package com.shigen.demo.util.serializer;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;

public class CustomPhoneSerializer extends JsonSerializer<String> {

  @Override
  public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if (StrUtil.isNotBlank(value) && PhoneUtil.isPhone(value)) {
      gen.writeString(PhoneUtil.hideBetween(value).toString());
    } else {
      gen.writeString("");
    }
  }
}
